package com.yang.controller;

import com.yang.mapper.RoleMapper;
import com.yang.pojo.Department;
import com.yang.pojo.Role;
import com.yang.pojo.UserInfo;
import com.yang.service.DepartmentService;
import com.yang.service.UserInfoService;
import com.yang.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/login")
public class LoginController {

	private static final Logger log = LoggerFactory.getLogger(LoginController.class);

	@Resource
	UserInfoService userInfoService;
	@Resource
	RoleMapper roleMapper;

	@PostMapping("/test1")
	public Map<String, Object> test1() throws ParseException {
		JsonResult jsonResult = new JsonResult();
		jsonResult.setCode("200");
		jsonResult.setMsg("测试成功");
		return jsonResult.getValues();
	}

	@PostMapping("/test2")
	public Map<String, Object> test2(@RequestParam("text") String text){
		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("text", text);
		return hashMap;
	}

	/**
	 * 员工登录
	 * @param username
	 * @param password
	 * @return
	 */
	@PostMapping("/userLogin")
	public Map<String,Object> userLogin(@RequestParam String username, @RequestParam String password) {
		System.out.println(username+"     "+password);
	Map<String, Object> map = new HashMap<>();
		try {
			UserInfo userInfoDB = userInfoService.findByUsername(username);
			if (userInfoDB != null) {
				if (userInfoDB.getRoleId() != 1){
					password = EncryptionUtil.password(password);
					if (password.equals(userInfoDB.getPassword())) {
						String sign = JwtUtil.sign(userInfoDB.getId(), userInfoDB.getUsername());

						map.put(ResponseKey.MESSAGE, "登录成功");
						map.put(ResponseKey.SUCCESS, true);
						map.put(ResponseKey.DATA, sign);
					} else {
						map.put(ResponseKey.MESSAGE, "登录失败");
						map.put(ResponseKey.SUCCESS, false);
					}
				}else {
					map.put(ResponseKey.MESSAGE, "登录失败");
					map.put(ResponseKey.SUCCESS, false);
				}
			} else {
				map.put(ResponseKey.MESSAGE, "登录失败");
				map.put(ResponseKey.SUCCESS, false);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			map.put(ResponseKey.MESSAGE, "登录失败");
			map.put(ResponseKey.SUCCESS, false);
		}
		return map;
	}

	/***
	 * 主管或者人事登录
	 * @param username
	 * @param password
	 * @return
	 */
	@PostMapping("/adminLogin")
	public Map<String,Object> adminLogin(@RequestParam String username, @RequestParam String password){
		JsonResult jsonResult = new JsonResult();
		try {
			UserInfo userInfoDB = userInfoService.findByUsername(username);
			if(userInfoDB != null){
				Role byId = roleMapper.findById(userInfoDB.getRoleId());
				if (byId.getName().equals("人事管理员") || byId.getName().equals("部门主管") || byId.getName().equals("系统管理员")){
					password = EncryptionUtil.password(password);
					if (password.equals(userInfoDB.getPassword())) {
						String sign = JwtUtil.sign(userInfoDB.getId(), userInfoDB.getUsername());

						jsonResult.setCode("200");
						jsonResult.setMsg("登录成功");
						jsonResult.setData(sign);
					} else {
						jsonResult.setMsg("用户名或密码错误");
						jsonResult.setCode("200");
					}
				}else{
					jsonResult.setMsg("无登录权限");
					jsonResult.setCode("200");
				}
			}else {
				jsonResult.setMsg("登录失败");
				jsonResult.setCode("200");
			}
		}catch (Exception e){
			e.printStackTrace();
			jsonResult.setMsg("系统开小差");
			jsonResult.setCode("500");
		}
		return jsonResult.getValues();
	}

	/**
	 * 注册
	 * @param username
	 * @param password
	 * @param phoneNumber
	 * @return
	 */
	@PostMapping("/userRegister")
	public Map<String,Object> userRegister(@RequestParam String username,@RequestParam String password,
			@RequestParam String phoneNumber) {
		Map<String, Object> map = new HashMap<>();
		try {
			UserInfo userInfoDB = userInfoService.findByUsername(username);
			if (userInfoDB == null) {
				UserInfo userInfo = new UserInfo();
				userInfo.setUsername(username);
				userInfo.setPassword(EncryptionUtil.password(password));
				userInfo.setPhoneNumber(phoneNumber);
				userInfo.setRoleId(Constants.ROLE_ID_NEW_REGISTER);
				userInfoService.save(userInfo);
				map.put(ResponseKey.MESSAGE, "注册成功");
				map.put(ResponseKey.SUCCESS, true);
			} else {
				map.put(ResponseKey.MESSAGE, "用户名已存在");
				map.put(ResponseKey.SUCCESS, false);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			map.put(ResponseKey.MESSAGE, "注册失败");
			map.put(ResponseKey.SUCCESS, false);
		}
		return map;
	}
}
